<!DOCTYPE html><html><head>
  <meta charset="utf-8">
  <base data-ice="baseUrl" href="/v4/../../">
  <title data-ice="title">Tutorial |  Sequelize | The node.js ORM for PostgreSQL, MySQL, SQLite and MSSQL</title>
  <link type="text/css" rel="stylesheet" href="/v4/css/style.css">
  <link type="text/css" rel="stylesheet" href="/v4/css/prettify-tomorrow.css">
  <script src="/v4/script/prettify/prettify.js"></script>
  
  
  <script src="/v4/script/manual.js"></script>
<script data-ice="userScript" src="/v4/user/script/0-script.js"></script>
<link data-ice="userStyle" rel="stylesheet" href="/v4/user/css/0-style.css">
<link data-ice="userStyle" rel="stylesheet" href="/v4/user/css/1-theme.css">
<link rel="shortcut icon" type="image/x-icon" href="/v4/favicon.ico"><meta name="robots" content="noindex"></head>
<body class="layout-container manual-root" data-ice="rootContainer">

<header><a href="/v4/"><img src="/v4/manual/asset/logo-small.png" class="header-logo"></a>
  <a href="/v4/./">Home</a>
  
  <a href="/v4/identifiers.html">Reference</a>
  <a href="/v4/source.html">Source</a>
  
  <a data-ice="repoURL" href="https://github.com/sequelize/sequelize.git" class="repo-url-github">Repository</a><a href="https://sequelize.org/slack" class="slack-link"><img class="slack-logo" src="/v4/manual/asset/slack.svg">Join us on Slack</a>
  <div class="search-box">
  <span>
    <img src="/v4/./image/search.png">
    <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span>
  </span>
    <ul class="search-result"></ul>
  </div>
</header>

<nav class="navigation" data-ice="nav"><div class="manual-toc-root">
  
<div data-ice="manual" data-toc-name="installation">
    <ul class="manual-toc">
      
    <li data-ice="manualNav" class="indent-h1 manual-color manual-color-installation" data-section-count="■■" data-link="manual/installation/getting-started.html"><a href="/v4/manual/installation/getting-started.html" data-ice="link">Getting started</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/installation/getting-started.html"><a href="/v4/manual/installation/getting-started.html#installation" data-ice="link">Installation</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/installation/getting-started.html"><a href="/v4/manual/installation/getting-started.html#setting-up-a-connection" data-ice="link">Setting up a connection</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/installation/getting-started.html"><a href="/v4/manual/installation/getting-started.html#test-the-connection" data-ice="link">Test the connection</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/installation/getting-started.html"><a href="/v4/manual/installation/getting-started.html#your-first-model" data-ice="link">Your first model</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/installation/getting-started.html"><a href="/v4/manual/installation/getting-started.html#your-first-query" data-ice="link">Your first query</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/installation/getting-started.html"><a href="/v4/manual/installation/getting-started.html#application-wide-model-options" data-ice="link">Application wide model options</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/installation/getting-started.html"><a href="/v4/manual/installation/getting-started.html#promises" data-ice="link">Promises</a></li>
<li data-ice="manualNav" class="indent-h1 manual-color manual-color-installation" data-section-count="■■" data-link="manual/installation/usage.html"><a href="/v4/manual/installation/usage.html" data-ice="link">Basic usage</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/installation/usage.html"><a href="/v4/manual/installation/usage.html#options" data-ice="link">Options</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/installation/usage.html"><a href="/v4/manual/installation/usage.html#read-replication" data-ice="link">Read replication</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/installation/usage.html"><a href="/v4/manual/installation/usage.html#dialects" data-ice="link">Dialects</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/installation/usage.html"><a href="/v4/manual/installation/usage.html#mysql" data-ice="link">MySQL</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/installation/usage.html"><a href="/v4/manual/installation/usage.html#sqlite" data-ice="link">SQLite</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/installation/usage.html"><a href="/v4/manual/installation/usage.html#postgresql" data-ice="link">PostgreSQL</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/installation/usage.html"><a href="/v4/manual/installation/usage.html#mssql" data-ice="link">MSSQL</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/installation/usage.html"><a href="/v4/manual/installation/usage.html#executing-raw-sql-queries" data-ice="link">Executing raw SQL queries</a></li>
</ul>
  </div>
<div data-ice="manual" data-toc-name="tutorial">
    <ul class="manual-toc">
      
    <li data-ice="manualNav" class="indent-h1 manual-color manual-color-tutorial" data-section-count="■■■■" data-link="manual/tutorial/models-definition.html"><a href="/v4/manual/tutorial/models-definition.html" data-ice="link">Model definition</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/models-definition.html"><a href="/v4/manual/tutorial/models-definition.html#timestamps" data-ice="link">Timestamps</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/models-definition.html"><a href="/v4/manual/tutorial/models-definition.html#data-types" data-ice="link">Data types</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-definition.html"><a href="/v4/manual/tutorial/models-definition.html#array-enum-" data-ice="link">Array(ENUM)</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-definition.html"><a href="/v4/manual/tutorial/models-definition.html#range-types" data-ice="link">Range types</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/models-definition.html"><a href="/v4/manual/tutorial/models-definition.html#deferrable" data-ice="link">Deferrable</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/models-definition.html"><a href="/v4/manual/tutorial/models-definition.html#getters-setters" data-ice="link">Getters &amp; setters</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-definition.html"><a href="/v4/manual/tutorial/models-definition.html#defining-as-part-of-a-property" data-ice="link">Defining as part of a property</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-definition.html"><a href="/v4/manual/tutorial/models-definition.html#defining-as-part-of-the-model-options" data-ice="link">Defining as part of the model options</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-definition.html"><a href="/v4/manual/tutorial/models-definition.html#helper-functions-for-use-inside-getter-and-setter-definitions" data-ice="link">Helper functions for use inside getter and setter definitions</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/models-definition.html"><a href="/v4/manual/tutorial/models-definition.html#validations" data-ice="link">Validations</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-definition.html"><a href="/v4/manual/tutorial/models-definition.html#validators-and-allownull-" data-ice="link">Validators and allowNull</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-definition.html"><a href="/v4/manual/tutorial/models-definition.html#model-validations" data-ice="link">Model validations</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/models-definition.html"><a href="/v4/manual/tutorial/models-definition.html#configuration" data-ice="link">Configuration</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/models-definition.html"><a href="/v4/manual/tutorial/models-definition.html#import" data-ice="link">Import</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/models-definition.html"><a href="/v4/manual/tutorial/models-definition.html#optimistic-locking" data-ice="link">Optimistic Locking</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/models-definition.html"><a href="/v4/manual/tutorial/models-definition.html#database-synchronization" data-ice="link">Database synchronization</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/models-definition.html"><a href="/v4/manual/tutorial/models-definition.html#expansion-of-models" data-ice="link">Expansion of models</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-definition.html"><a href="/v4/manual/tutorial/models-definition.html#indexes" data-ice="link">Indexes</a></li>
<li data-ice="manualNav" class="indent-h1 manual-color manual-color-tutorial" data-section-count="■■■■" data-link="manual/tutorial/models-usage.html"><a href="/v4/manual/tutorial/models-usage.html" data-ice="link">Model usage</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/models-usage.html"><a href="/v4/manual/tutorial/models-usage.html#data-retrieval-finders" data-ice="link">Data retrieval / Finders</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-usage.html"><a href="/v4/manual/tutorial/models-usage.html#-find-search-for-one-specific-element-in-the-database" data-ice="link">find - Search for one specific element in the database</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-usage.html"><a href="/v4/manual/tutorial/models-usage.html#-findorcreate-search-for-a-specific-element-or-create-it-if-not-available" data-ice="link">findOrCreate - Search for a specific element or create it if not available</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-usage.html"><a href="/v4/manual/tutorial/models-usage.html#-findandcountall-search-for-multiple-elements-in-the-database-returns-both-data-and-total-count" data-ice="link">findAndCountAll - Search for multiple elements in the database, returns both data and total count</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-usage.html"><a href="/v4/manual/tutorial/models-usage.html#-findall-search-for-multiple-elements-in-the-database" data-ice="link">findAll - Search for multiple elements in the database</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-usage.html"><a href="/v4/manual/tutorial/models-usage.html#complex-filtering-or-not-queries" data-ice="link">Complex filtering / OR / NOT queries</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-usage.html"><a href="/v4/manual/tutorial/models-usage.html#manipulating-the-dataset-with-limit-offset-order-and-group" data-ice="link">Manipulating the dataset with limit, offset, order and group</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-usage.html"><a href="/v4/manual/tutorial/models-usage.html#raw-queries" data-ice="link">Raw queries</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-usage.html"><a href="/v4/manual/tutorial/models-usage.html#-count-count-the-occurrences-of-elements-in-the-database" data-ice="link">count - Count the occurrences of elements in the database</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-usage.html"><a href="/v4/manual/tutorial/models-usage.html#-max-get-the-greatest-value-of-a-specific-attribute-within-a-specific-table" data-ice="link">max - Get the greatest value of a specific attribute within a specific table</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-usage.html"><a href="/v4/manual/tutorial/models-usage.html#-min-get-the-least-value-of-a-specific-attribute-within-a-specific-table" data-ice="link">min - Get the least value of a specific attribute within a specific table</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-usage.html"><a href="/v4/manual/tutorial/models-usage.html#-sum-sum-the-value-of-specific-attributes" data-ice="link">sum - Sum the value of specific attributes</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/models-usage.html"><a href="/v4/manual/tutorial/models-usage.html#eager-loading" data-ice="link">Eager loading</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-usage.html"><a href="/v4/manual/tutorial/models-usage.html#top-level-where-with-eagerly-loaded-models" data-ice="link">Top level where with eagerly loaded models</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-usage.html"><a href="/v4/manual/tutorial/models-usage.html#including-everything" data-ice="link">Including everything</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-usage.html"><a href="/v4/manual/tutorial/models-usage.html#including-soft-deleted-records" data-ice="link">Including soft deleted records</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-usage.html"><a href="/v4/manual/tutorial/models-usage.html#ordering-eager-loaded-associations" data-ice="link">Ordering Eager Loaded Associations</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/models-usage.html"><a href="/v4/manual/tutorial/models-usage.html#nested-eager-loading" data-ice="link">Nested eager loading</a></li>
<li data-ice="manualNav" class="indent-h1 manual-color manual-color-tutorial" data-section-count="■■■■■" data-link="manual/tutorial/querying.html"><a href="/v4/manual/tutorial/querying.html" data-ice="link">Querying</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/querying.html"><a href="/v4/manual/tutorial/querying.html#attributes" data-ice="link">Attributes</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/querying.html"><a href="/v4/manual/tutorial/querying.html#where" data-ice="link">Where</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/querying.html"><a href="/v4/manual/tutorial/querying.html#basics" data-ice="link">Basics</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/querying.html"><a href="/v4/manual/tutorial/querying.html#operators" data-ice="link">Operators</a></li>
<li data-ice="manualNav" class="indent-h4" data-link="manual/tutorial/querying.html"><a href="/v4/manual/tutorial/querying.html#range-operators" data-ice="link">Range Operators</a></li>
<li data-ice="manualNav" class="indent-h4" data-link="manual/tutorial/querying.html"><a href="/v4/manual/tutorial/querying.html#combinations" data-ice="link">Combinations</a></li>
<li data-ice="manualNav" class="indent-h4" data-link="manual/tutorial/querying.html"><a href="/v4/manual/tutorial/querying.html#operators-aliases" data-ice="link">Operators Aliases</a></li>
<li data-ice="manualNav" class="indent-h4" data-link="manual/tutorial/querying.html"><a href="/v4/manual/tutorial/querying.html#operators-security" data-ice="link">Operators security</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/querying.html"><a href="/v4/manual/tutorial/querying.html#json" data-ice="link">JSON</a></li>
<li data-ice="manualNav" class="indent-h4" data-link="manual/tutorial/querying.html"><a href="/v4/manual/tutorial/querying.html#postgresql" data-ice="link">PostgreSQL</a></li>
<li data-ice="manualNav" class="indent-h4" data-link="manual/tutorial/querying.html"><a href="/v4/manual/tutorial/querying.html#mssql" data-ice="link">MSSQL</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/querying.html"><a href="/v4/manual/tutorial/querying.html#jsonb" data-ice="link">JSONB</a></li>
<li data-ice="manualNav" class="indent-h4" data-link="manual/tutorial/querying.html"><a href="/v4/manual/tutorial/querying.html#nested-object" data-ice="link">Nested object</a></li>
<li data-ice="manualNav" class="indent-h4" data-link="manual/tutorial/querying.html"><a href="/v4/manual/tutorial/querying.html#nested-key" data-ice="link">Nested key</a></li>
<li data-ice="manualNav" class="indent-h4" data-link="manual/tutorial/querying.html"><a href="/v4/manual/tutorial/querying.html#containment" data-ice="link">Containment</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/querying.html"><a href="/v4/manual/tutorial/querying.html#relations-associations" data-ice="link">Relations / Associations</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/querying.html"><a href="/v4/manual/tutorial/querying.html#pagination-limiting" data-ice="link">Pagination / Limiting</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/querying.html"><a href="/v4/manual/tutorial/querying.html#ordering" data-ice="link">Ordering</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/querying.html"><a href="/v4/manual/tutorial/querying.html#table-hint" data-ice="link">Table Hint</a></li>
<li data-ice="manualNav" class="indent-h1 manual-color manual-color-tutorial" data-section-count="■■■" data-link="manual/tutorial/instances.html"><a href="/v4/manual/tutorial/instances.html" data-ice="link">Instances</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/instances.html"><a href="/v4/manual/tutorial/instances.html#building-a-non-persistent-instance" data-ice="link">Building a non-persistent instance</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/instances.html"><a href="/v4/manual/tutorial/instances.html#creating-persistent-instances" data-ice="link">Creating persistent instances</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/instances.html"><a href="/v4/manual/tutorial/instances.html#updating-saving-persisting-an-instance" data-ice="link">Updating / Saving / Persisting an instance</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/instances.html"><a href="/v4/manual/tutorial/instances.html#destroying-deleting-persistent-instances" data-ice="link">Destroying / Deleting persistent instances</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/instances.html"><a href="/v4/manual/tutorial/instances.html#working-in-bulk-creating-updating-and-destroying-multiple-rows-at-once-" data-ice="link">Working in bulk (creating, updating and destroying multiple rows at once)</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/instances.html"><a href="/v4/manual/tutorial/instances.html#values-of-an-instance" data-ice="link">Values of an instance</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/instances.html"><a href="/v4/manual/tutorial/instances.html#reloading-instances" data-ice="link">Reloading instances</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/instances.html"><a href="/v4/manual/tutorial/instances.html#incrementing" data-ice="link">Incrementing</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/instances.html"><a href="/v4/manual/tutorial/instances.html#decrementing" data-ice="link">Decrementing</a></li>
<li data-ice="manualNav" class="indent-h1 manual-color manual-color-tutorial" data-section-count="■■■■■" data-link="manual/tutorial/associations.html"><a href="/v4/manual/tutorial/associations.html" data-ice="link">Associations</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/associations.html"><a href="/v4/manual/tutorial/associations.html#one-to-one-associations" data-ice="link">One-To-One associations</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/associations.html"><a href="/v4/manual/tutorial/associations.html#belongsto" data-ice="link">BelongsTo</a></li>
<li data-ice="manualNav" class="indent-h4" data-link="manual/tutorial/associations.html"><a href="/v4/manual/tutorial/associations.html#foreign-keys" data-ice="link">Foreign keys</a></li>
<li data-ice="manualNav" class="indent-h4" data-link="manual/tutorial/associations.html"><a href="/v4/manual/tutorial/associations.html#target-keys" data-ice="link">Target keys</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/associations.html"><a href="/v4/manual/tutorial/associations.html#hasone" data-ice="link">HasOne</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/associations.html"><a href="/v4/manual/tutorial/associations.html#difference-between-hasone-and-belongsto" data-ice="link">Difference between HasOne and BelongsTo</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/associations.html"><a href="/v4/manual/tutorial/associations.html#one-to-many-associations-hasmany-" data-ice="link">One-To-Many associations (hasMany)</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/associations.html"><a href="/v4/manual/tutorial/associations.html#belongs-to-many-associations" data-ice="link">Belongs-To-Many associations</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/associations.html"><a href="/v4/manual/tutorial/associations.html#scopes" data-ice="link">Scopes</a></li>
<li data-ice="manualNav" class="indent-h4" data-link="manual/tutorial/associations.html"><a href="/v4/manual/tutorial/associations.html#1-m" data-ice="link">1:m</a></li>
<li data-ice="manualNav" class="indent-h4" data-link="manual/tutorial/associations.html"><a href="/v4/manual/tutorial/associations.html#n-m" data-ice="link">n:m</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/associations.html"><a href="/v4/manual/tutorial/associations.html#naming-strategy" data-ice="link">Naming strategy</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/associations.html"><a href="/v4/manual/tutorial/associations.html#associating-objects" data-ice="link">Associating objects</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/associations.html"><a href="/v4/manual/tutorial/associations.html#check-associations" data-ice="link">Check associations</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/associations.html"><a href="/v4/manual/tutorial/associations.html#foreign-keys" data-ice="link">Foreign Keys</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/associations.html"><a href="/v4/manual/tutorial/associations.html#enforcing-a-foreign-key-reference-without-constraints" data-ice="link">Enforcing a foreign key reference without constraints</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/associations.html"><a href="/v4/manual/tutorial/associations.html#creating-with-associations" data-ice="link">Creating with associations</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/associations.html"><a href="/v4/manual/tutorial/associations.html#creating-elements-of-a-belongsto-has-many-or-hasone-association" data-ice="link">Creating elements of a "BelongsTo", "Has Many" or "HasOne" association</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/associations.html"><a href="/v4/manual/tutorial/associations.html#creating-elements-of-a-belongsto-association-with-an-alias" data-ice="link">Creating elements of a "BelongsTo" association with an alias</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/associations.html"><a href="/v4/manual/tutorial/associations.html#creating-elements-of-a-hasmany-or-belongstomany-association" data-ice="link">Creating elements of a "HasMany" or "BelongsToMany" association</a></li>
<li data-ice="manualNav" class="indent-h1 manual-color manual-color-tutorial" data-section-count="■■■" data-link="manual/tutorial/transactions.html"><a href="/v4/manual/tutorial/transactions.html" data-ice="link">Transactions</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/transactions.html"><a href="/v4/manual/tutorial/transactions.html#managed-transaction-auto-callback-" data-ice="link">Managed transaction (auto-callback)</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/transactions.html"><a href="/v4/manual/tutorial/transactions.html#throw-errors-to-rollback" data-ice="link">Throw errors to rollback</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/transactions.html"><a href="/v4/manual/tutorial/transactions.html#automatically-pass-transactions-to-all-queries" data-ice="link">Automatically pass transactions to all queries</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/transactions.html"><a href="/v4/manual/tutorial/transactions.html#concurrent-partial-transactions" data-ice="link">Concurrent/Partial transactions</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/transactions.html"><a href="/v4/manual/tutorial/transactions.html#without-cls-enabled" data-ice="link">Without CLS enabled</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/transactions.html"><a href="/v4/manual/tutorial/transactions.html#isolation-levels" data-ice="link">Isolation levels</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/transactions.html"><a href="/v4/manual/tutorial/transactions.html#unmanaged-transaction-then-callback-" data-ice="link">Unmanaged transaction (then-callback)</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/transactions.html"><a href="/v4/manual/tutorial/transactions.html#options" data-ice="link">Options</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/transactions.html"><a href="/v4/manual/tutorial/transactions.html#usage-with-other-sequelize-methods" data-ice="link">Usage with other sequelize methods</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/transactions.html"><a href="/v4/manual/tutorial/transactions.html#after-commit-hook" data-ice="link">After commit hook</a></li>
<li data-ice="manualNav" class="indent-h1 manual-color manual-color-tutorial" data-section-count="■■" data-link="manual/tutorial/scopes.html"><a href="/v4/manual/tutorial/scopes.html" data-ice="link">Scopes</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/scopes.html"><a href="/v4/manual/tutorial/scopes.html#definition" data-ice="link">Definition</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/scopes.html"><a href="/v4/manual/tutorial/scopes.html#usage" data-ice="link">Usage</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/scopes.html"><a href="/v4/manual/tutorial/scopes.html#merging" data-ice="link">Merging</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/scopes.html"><a href="/v4/manual/tutorial/scopes.html#associations" data-ice="link">Associations</a></li>
<li data-ice="manualNav" class="indent-h1 manual-color manual-color-tutorial" data-section-count="■■■" data-link="manual/tutorial/hooks.html"><a href="/v4/manual/tutorial/hooks.html" data-ice="link">Hooks</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/hooks.html"><a href="/v4/manual/tutorial/hooks.html#order-of-operations" data-ice="link">Order of Operations</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/hooks.html"><a href="/v4/manual/tutorial/hooks.html#declaring-hooks" data-ice="link">Declaring Hooks</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/hooks.html"><a href="/v4/manual/tutorial/hooks.html#removing-hooks" data-ice="link">Removing hooks</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/hooks.html"><a href="/v4/manual/tutorial/hooks.html#global-universal-hooks" data-ice="link">Global / universal hooks</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/hooks.html"><a href="/v4/manual/tutorial/hooks.html#sequelize-options-define-default-hook-" data-ice="link">Sequelize.options.define (default hook)</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/hooks.html"><a href="/v4/manual/tutorial/hooks.html#sequelize-addhook-permanent-hook-" data-ice="link">Sequelize.addHook (permanent hook)</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/hooks.html"><a href="/v4/manual/tutorial/hooks.html#instance-hooks" data-ice="link">Instance hooks</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/hooks.html"><a href="/v4/manual/tutorial/hooks.html#model-hooks" data-ice="link">Model hooks</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/hooks.html"><a href="/v4/manual/tutorial/hooks.html#associations" data-ice="link">Associations</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/hooks.html"><a href="/v4/manual/tutorial/hooks.html#a-note-about-transactions" data-ice="link">A Note About Transactions</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/hooks.html"><a href="/v4/manual/tutorial/hooks.html#internal-transactions" data-ice="link">Internal Transactions</a></li>
<li data-ice="manualNav" class="indent-h1 manual-color manual-color-tutorial" data-section-count="■" data-link="manual/tutorial/raw-queries.html"><a href="/v4/manual/tutorial/raw-queries.html" data-ice="link">Raw queries</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/raw-queries.html"><a href="/v4/manual/tutorial/raw-queries.html#replacements" data-ice="link">Replacements</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/raw-queries.html"><a href="/v4/manual/tutorial/raw-queries.html#bind-parameter" data-ice="link">Bind Parameter</a></li>
<li data-ice="manualNav" class="indent-h1 manual-color manual-color-tutorial" data-section-count="■■■■■" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html" data-ice="link">Migrations</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#the-cli" data-ice="link">The CLI</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#installing-cli" data-ice="link">Installing CLI</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#bootstrapping" data-ice="link">Bootstrapping</a></li>
<li data-ice="manualNav" class="indent-h4" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#configuration" data-ice="link">Configuration</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#creating-first-model-and-migration-" data-ice="link">Creating first Model (and Migration)</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#running-migrations" data-ice="link">Running Migrations</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#undoing-migrations" data-ice="link">Undoing Migrations</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#creating-first-seed" data-ice="link">Creating First Seed</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#running-seeds" data-ice="link">Running Seeds</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#undoing-seeds" data-ice="link">Undoing Seeds</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#advance-topics" data-ice="link">Advance Topics</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#migration-skeleton" data-ice="link">Migration Skeleton</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#the-sequelizerc-file" data-ice="link">The .sequelizerc File</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#dynamic-configuration" data-ice="link">Dynamic Configuration</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#using-environment-variables" data-ice="link">Using Environment Variables</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#specifying-dialect-options" data-ice="link">Specifying Dialect Options</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#production-usages" data-ice="link">Production Usages</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#storage" data-ice="link">Storage</a></li>
<li data-ice="manualNav" class="indent-h4" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#migration-storage" data-ice="link">Migration Storage</a></li>
<li data-ice="manualNav" class="indent-h4" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#seed-storage" data-ice="link">Seed Storage</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#configuration-connection-string" data-ice="link">Configuration Connection String</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#connecting-over-ssl" data-ice="link">Connecting over SSL</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#programmatic-use" data-ice="link">Programmatic use</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/migrations.html"><a href="/v4/manual/tutorial/migrations.html#query-interface" data-ice="link">Query Interface</a></li>
<li data-ice="manualNav" class="indent-h1 manual-color manual-color-tutorial" data-section-count="■■■" data-link="manual/tutorial/upgrade-to-v4.html"><a href="/v4/manual/tutorial/upgrade-to-v4.html" data-ice="link">Upgrade to V4</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/upgrade-to-v4.html"><a href="/v4/manual/tutorial/upgrade-to-v4.html#changelog" data-ice="link">Changelog</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/tutorial/upgrade-to-v4.html"><a href="/v4/manual/tutorial/upgrade-to-v4.html#breaking-changes" data-ice="link">Breaking Changes</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/upgrade-to-v4.html"><a href="/v4/manual/tutorial/upgrade-to-v4.html#node" data-ice="link">Node</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/upgrade-to-v4.html"><a href="/v4/manual/tutorial/upgrade-to-v4.html#general" data-ice="link">General</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/upgrade-to-v4.html"><a href="/v4/manual/tutorial/upgrade-to-v4.html#config-options" data-ice="link">Config / Options</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/upgrade-to-v4.html"><a href="/v4/manual/tutorial/upgrade-to-v4.html#data-types" data-ice="link">Data Types</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/upgrade-to-v4.html"><a href="/v4/manual/tutorial/upgrade-to-v4.html#transactions-cls" data-ice="link">Transactions / CLS</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/upgrade-to-v4.html"><a href="/v4/manual/tutorial/upgrade-to-v4.html#raw-queries" data-ice="link">Raw Queries</a></li>
<li data-ice="manualNav" class="indent-h3" data-link="manual/tutorial/upgrade-to-v4.html"><a href="/v4/manual/tutorial/upgrade-to-v4.html#others" data-ice="link">Others</a></li>
</ul>
  </div>
<div data-ice="manual" data-toc-name="advanced">
    <ul class="manual-toc">
      
    <li data-ice="manualNav" class="indent-h1 manual-color manual-color-advanced" data-section-count="■■" data-link="manual/advanced/legacy.html"><a href="/v4/manual/advanced/legacy.html" data-ice="link">Working with legacy tables</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/advanced/legacy.html"><a href="/v4/manual/advanced/legacy.html#tables" data-ice="link">Tables</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/advanced/legacy.html"><a href="/v4/manual/advanced/legacy.html#fields" data-ice="link">Fields</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/advanced/legacy.html"><a href="/v4/manual/advanced/legacy.html#primary-keys" data-ice="link">Primary keys</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/advanced/legacy.html"><a href="/v4/manual/advanced/legacy.html#foreign-keys" data-ice="link">Foreign keys</a></li>
</ul>
  </div>
<div data-ice="manual" data-toc-name="reference">
    <ul class="manual-toc">
      
    <li data-ice="manualNav" class="indent-h1 manual-color manual-color-reference" data-section-count="■■■■■" data-link="identifiers.html"><a href="/v4/identifiers.html" data-ice="link">Reference</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="identifiers.html"><a href="/v4/identifiers.html#class" data-ice="link">Class</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="identifiers.html"><a href="/v4/identifiers.html#function" data-ice="link">Function</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="identifiers.html"><a href="/v4/identifiers.html#variable" data-ice="link">Variable</a></li>
</ul>
  </div>
<div data-ice="manual" data-toc-name="faq">
    <ul class="manual-toc">
      
    <li data-ice="manualNav" class="indent-h1 manual-color manual-color-faq" data-section-count="■" data-link="manual/faq/whos-using.html"><a href="/v4/manual/faq/whos-using.html" data-ice="link">Who's using sequelize?</a></li>
<li data-ice="manualNav" class="indent-h1 manual-color manual-color-faq" data-section-count="■" data-link="manual/faq/imprint.html"><a href="/v4/manual/faq/imprint.html" data-ice="link">Imprint</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/faq/imprint.html"><a href="/v4/manual/faq/imprint.html#author-s-" data-ice="link">AUTHOR(S)</a></li>
<li data-ice="manualNav" class="indent-h2" data-link="manual/faq/imprint.html"><a href="/v4/manual/faq/imprint.html#inhaltliche-verantwortung" data-ice="link">INHALTLICHE VERANTWORTUNG</a></li>
</ul>
  </div>
</div>
</nav>

<div class="content" data-ice="content"><div class="github-markdown">
  <div class="manual-breadcrumb-list">
    <a href="/v4/./manual/./index.html">Manual</a>
    <span>»</span>
    <span data-ice="title">Tutorial</span>
  </div>
  <div data-ice="content"><h1 id="migrations">Migrations</h1>
<p>Just like you use Git / SVN to manage changes in your source code, you can use migrations to keep track of changes to the database. With migrations you can transfer your existing database into another state and vice versa: Those state transitions are saved in migration files, which describe how to get to the new state and how to revert the changes in order to get back to the old state.</p>
<p>You will need <a href="https://github.com/sequelize/cli">Sequelize CLI</a>. The CLI ships support for migrations and project bootstrapping.</p>
<h2 id="the-cli">The CLI</h2>
<h3 id="installing-cli">Installing CLI</h3>
<p>Let's start with installing CLI, you can find instructions <a href="https://github.com/sequelize/cli">here</a>. Most preferred way is installing locally like this</p>
<pre><code class="lang-bash"><code class="source-code prettyprint">$ npm install --save sequelize-cli</code>
</code></pre>
<h3 id="bootstrapping">Bootstrapping</h3>
<p>To create an empty project you will need to execute <code>init</code> command</p>
<pre><code class="lang-bash"><code class="source-code prettyprint">$ node_modules/.bin/sequelize init</code>
</code></pre>
<p>This will create following folders</p>
<ul>
<li><code>config</code>, contains config file, which tells CLI how to connect with database</li>
<li><code>models</code>, contains all models for your project</li>
<li><code>migrations</code>, contains all migration files</li>
<li><code>seeders</code>, contains all seed files</li>
</ul>
<h4 id="configuration">Configuration</h4>
<p>Before continuing further we will need to tell CLI how to connect to database. To do that let's open default config file <code>config/config.json</code>. It looks something like this</p>
<pre><code class="lang-json"><code class="source-code prettyprint">{
  "development": {
    "username": "root",
    "password": null,
    "database": "database_development",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "test": {
    "username": "root",
    "password": null,
    "database": "database_test",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "production": {
    "username": "root",
    "password": null,
    "database": "database_test",
    "host": "127.0.0.1",
    "dialect": "mysql"
  }
}</code>
</code></pre>
<p>Now edit this file and set correct database credentials and dialect.</p>
<p><strong>Note:</strong> <em>If your database doesn't exists yet, you can just call <code>db:create</code> command. With proper access it will create that database for you.</em></p>
<h3 id="creating-first-model-and-migration-">Creating first Model (and Migration)</h3>
<p>Once you have properly configured CLI config file you are ready to create your first migration. It's as simple as executing a simple command.</p>
<p>We will use <code>model:generate</code> command. This command requires two options</p>
<ul>
<li><code>name</code>, Name of the model</li>
<li><code>attributes</code>, List of model attributes</li>
</ul>
<p>Let's create a model named <code>User</code>.</p>
<pre><code class="lang-bash"><code class="source-code prettyprint">$ node_modules/.bin/sequelize model:generate --name User --attributes firstName:string,lastName:string,email:string</code>
</code></pre>
<p>This will do following</p>
<ul>
<li>Create a model file <code>user</code> in <code>models</code> folder</li>
<li>Create a migration file with name like <code>XXXXXXXXXXXXXX-create-user.js</code> in <code>migrations</code> folder</li>
</ul>
<p><strong>Note:</strong> <em>Sequelize will only use Model files, it's the table representation. On the other hand, the migration file is a change in that model or more specifically that table, used by CLI. Treat migrations like a commit or a log for some change in database.</em></p>
<h3 id="running-migrations">Running Migrations</h3>
<p>Until this step, we haven't inserted anything into the database. We have just created required model and migration files for our first model <code>User</code>. Now to actually create that table in database you need to run <code>db:migrate</code> command.</p>
<pre><code class="lang-bash"><code class="source-code prettyprint">$ node_modules/.bin/sequelize db:migrate</code>
</code></pre>
<p>This command will execute these steps:</p>
<ul>
<li>Will ensure a table called <code>SequelizeMeta</code> in database. This table is used to record which migrations have run on the current database</li>
<li>Start looking for any migration files which haven't run yet. This is possible by checking <code>SequelizeMeta</code> table. In this case it will run <code>XXXXXXXXXXXXXX-create-user.js</code> migration, which we created in last step.</li>
<li>Creates a table called <code>Users</code> with all columns as specified in its migration file.</li>
</ul>
<h3 id="undoing-migrations">Undoing Migrations</h3>
<p>Now our table has been created and saved in database. With migration you can revert to old state by just running a command.</p>
<p>You can use <code>db:migrate:undo</code>, this command will revert most recent migration.</p>
<pre><code class="lang-bash"><code class="source-code prettyprint">$ node_modules/.bin/sequelize db:migrate:undo</code>
</code></pre>
<p>You can revert back to initial state by undoing all migrations with <code>db:migrate:undo:all</code> command. You can also revert back to a specific migration by passing its name in <code>--to</code> option.</p>
<pre><code class="lang-bash"><code class="source-code prettyprint">$ node_modules/.bin/sequelize db:migrate:undo:all --to XXXXXXXXXXXXXX-create-posts.js</code>
</code></pre>
<h3 id="creating-first-seed">Creating First Seed</h3>
<p>Suppose we want to insert some data into a few tables by default. If we follow up on previous example we can consider creating a demo user for <code>User</code> table.</p>
<p>To manage all data migrations you can use seeders. Seed files are some change in data that can be used to populate database table with sample data or test data.</p>
<p>Let's create a seed file which will add a demo user to our <code>User</code> table.</p>
<pre><code class="lang-bash"><code class="source-code prettyprint">$ node_modules/.bin/sequelize seed:generate --name demo-user</code>
</code></pre>
<p>This command will create a seed file in <code>seeders</code> folder. File name will look something like <code>XXXXXXXXXXXXXX-demo-user.js</code>. It follows the same <code>up / down</code> semantics as the migration files.</p>
<p>Now we should edit this file to insert demo user to <code>User</code> table.</p>
<pre><code class="lang-js"><code class="source-code prettyprint">'use strict';

module.exports = {
  up: (queryInterface, Sequelize) =&gt; {
    return queryInterface.bulkInsert('Users', [{
        firstName: 'John',
        lastName: 'Doe',
        email: 'demo@demo.com'
      }], {});
  },

  down: (queryInterface, Sequelize) =&gt; {
    return queryInterface.bulkDelete('Users', null, {});
  }
};</code>
</code></pre>
<h3 id="running-seeds">Running Seeds</h3>
<p>In last step you have create a seed file. It's still not committed to database. To do that we need to run a simple command.</p>
<pre><code class="lang-bash"><code class="source-code prettyprint">$ node_modules/.bin/sequelize db:seed:all</code>
</code></pre>
<p>This will execute that seed file and you will have a demo user inserted into <code>User</code> table.</p>
<p><strong>Note:</strong> <em>Seeders execution is not stored anywhere unlike migrations, which use the <code>SequelizeMeta</code> table. If you wish to override this please read <code>Storage</code> section</em></p>
<h3 id="undoing-seeds">Undoing Seeds</h3>
<p>Seeders can be undone if they are using any storage. There are two commands available for that:</p>
<p>If you wish to undo most recent seed</p>
<pre><code class="lang-bash"><code class="source-code prettyprint">node_modules/.bin/sequelize db:seed:undo</code>
</code></pre>
<p>If you wish to undo all seeds</p>
<pre><code class="lang-bash"><code class="source-code prettyprint">node_modules/.bin/sequelize db:seed:undo:all</code>
</code></pre>
<h2 id="advance-topics">Advance Topics</h2>
<h3 id="migration-skeleton">Migration Skeleton</h3>
<p>The following skeleton shows a typical migration file.</p>
<pre><code class="lang-js"><code class="source-code prettyprint">module.exports = {
  up: (queryInterface, Sequelize) =&gt; {
    // logic for transforming into the new state
  },

  down: (queryInterface, Sequelize) =&gt; {
    // logic for reverting the changes
  }
}</code>
</code></pre>
<p>The passed <code>queryInterface</code> object can be used to modify the database. The <code>Sequelize</code> object stores the available data types such as <code>STRING</code> or <code>INTEGER</code>. Function <code>up</code> or <code>down</code> should return a <code>Promise</code>. Let's look at an example:</p>
<pre><code class="lang-js"><code class="source-code prettyprint">module.exports = {
  up: (queryInterface, Sequelize) =&gt; {
    return queryInterface.createTable('Person', {
        name: Sequelize.STRING,
        isBetaMember: {
          type: Sequelize.BOOLEAN,
          defaultValue: false,
          allowNull: false
        }
      });
  },
  down: (queryInterface, Sequelize) =&gt; {
    return queryInterface.dropTable('Person');
  }
}</code>
</code></pre>
<h3 id="the-sequelizerc-file">The <code>.sequelizerc</code> File</h3>
<p>This is a special configuration file. It lets you specify various options that you would usually pass as arguments to CLI. Some scenarios where you can use it.</p>
<ul>
<li>You want to override default path to <code>migrations</code>, <code>models</code>, <code>seeders</code> or <code>config</code> folder.</li>
<li>You want to rename <code>config.json</code> to something else like <code>database.json</code></li>
</ul>
<p>And a whole lot more. Let's see how you can use this file for custom configuration.</p>
<p>For starters, let's create an empty file in root directory of your project.</p>
<pre><code class="lang-bash"><code class="source-code prettyprint">$ touch .sequelizerc</code>
</code></pre>
<p>Now let's work with an example config.</p>
<pre><code class="lang-js"><code class="source-code prettyprint">const path = require('path');

module.exports = {
  'config': path.resolve('config', 'database.json'),
  'models-path': path.resolve('db', 'models'),
  'seeders-path': path.resolve('db', 'seeders'),
  'migrations-path': path.resolve('db', 'migrations')
}</code>
</code></pre>
<p>With this config you are telling CLI to</p>
<ul>
<li>Use <code>config/database.json</code> file for config settings</li>
<li>Use <code>db/models</code> as models folder</li>
<li>Use <code>db/seeders</code> as seeders folder</li>
<li>Use <code>db/migrations</code> as migrations folder</li>
</ul>
<h3 id="dynamic-configuration">Dynamic Configuration</h3>
<p>Configuration file is by default a JSON file called <code>config.json</code>. But sometimes you want to execute some code or access environment variables which is not possible in JSON files.</p>
<p>Sequelize CLI can read from both <code>JSON</code> and <code>JS</code> files. This can be setup with <code>.sequelizerc</code> file. Let see how</p>
<p>First you need to create a <code>.sequelizerc</code> file in root folder of your project. This file should override config path to a <code>JS</code> file. Like this</p>
<pre><code class="lang-js"><code class="source-code prettyprint">const path = require('path');

module.exports = {
  'config': path.resolve('config', 'config.js')
}</code>
</code></pre>
<p>Now Sequelize CLI will load <code>config/config.js</code> for getting configuration options. Since this is a JS file you can have any code executed and export final dynamic configuration file.</p>
<p>An example of <code>config/config.js</code> file</p>
<pre><code class="lang-js"><code class="source-code prettyprint">const fs = require('fs');

module.exports = {
  development: {
    username: 'database_dev',
    password: 'database_dev',
    database: 'database_dev',
    host: '127.0.0.1',
    dialect: 'mysql'
  },
  test: {
    username: 'database_test',
    password: null,
    database: 'database_test',
    host: '127.0.0.1',
    dialect: 'mysql'
  },
  production: {
    username: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    host: process.env.DB_HOSTNAME,
    dialect: 'mysql',
    dialectOptions: {
      ssl: {
        ca: fs.readFileSync(__dirname + '/mysql-ca-master.crt')
      }
    }
  }
};</code>
</code></pre>
<h3 id="using-environment-variables">Using Environment Variables</h3>
<p>With CLI you can directly access the environment variables inside the <code>config/config.js</code>. You can use <code>.sequelizerc</code> to tell CLI to use <code>config/config.js</code> for configuration. This is explained in last section.</p>
<p>Then you can just expose file with proper environment variables.</p>
<pre><code class="lang-js"><code class="source-code prettyprint">module.exports = {
  development: {
    username: 'database_dev',
    password: 'database_dev',
    database: 'database_dev',
    host: '127.0.0.1',
    dialect: 'mysql'
  },
  test: {
    username: process.env.CI_DB_USERNAME,
    password: process.env.CI_DB_PASSWORD,
    database: process.env.CI_DB_NAME,
    host: '127.0.0.1',
    dialect: 'mysql'
  },
  production: {
    username: process.env.PROD_DB_USERNAME,
    password: process.env.PROD_DB_PASSWORD,
    database: process.env.PROD_DB_NAME,
    host: process.env.PROD_DB_HOSTNAME,
    dialect: 'mysql'
  }</code>
</code></pre>
<h3 id="specifying-dialect-options">Specifying Dialect Options</h3>
<p>Sometime you want to specify a dialectOption, if it's a general config you can just add it in <code>config/config.json</code>. Sometime you want to execute some code to get dialectOptions, you should use dynamic config file for those cases.</p>
<pre><code class="lang-json"><code class="source-code prettyprint">{
    "production": {
        "dialect":"mysql",
        "dialectOptions": {
            "bigNumberStrings": true
        }
    }
}</code>
</code></pre>
<h3 id="production-usages">Production Usages</h3>
<p>Some tips around using CLI and migration setup in production environment.</p>
<p>1) Use environment variables for config settings. This is better achieved with dynamic configuration. A sample production safe configuration may look like.</p>
<pre><code class="lang-js"><code class="source-code prettyprint">const fs = require('fs');

module.exports = {
  development: {
    username: 'database_dev',
    password: 'database_dev',
    database: 'database_dev',
    host: '127.0.0.1',
    dialect: 'mysql'
  },
  test: {
    username: 'database_test',
    password: null,
    database: 'database_test',
    host: '127.0.0.1',
    dialect: 'mysql'
  },
  production: {
    username: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_NAME,
    host: process.env.DB_HOSTNAME,
    dialect: 'mysql',
    dialectOptions: {
      ssl: {
        ca: fs.readFileSync(__dirname + '/mysql-ca-master.crt')
      }
    }
  }
};</code>
</code></pre>
<p>Our goal is to use environment variables for various database secrets and not accidentally check them in to source control.</p>
<h3 id="storage">Storage</h3>
<p>There are three types of storage that you can use: <code>sequelize</code>, <code>json</code>, and <code>none</code>.</p>
<ul>
<li><code>sequelize</code> : stores migrations and seeds in a table on the sequelize database</li>
<li><code>json</code> : stores migrations and seeds on a json file</li>
<li><code>none</code> : does not store any migration/seed</li>
</ul>
<h4 id="migration-storage">Migration Storage</h4>
<p>By default the CLI will create a table in your database called <code>SequelizeMeta</code> containing an entry
for each executed migration. To change this behavior, there are three options you can add to the
configuration file. Using <code>migrationStorage</code>, you can choose the type of storage to be used for
migrations. If you choose <code>json</code>, you can specify the path of the file using <code>migrationStoragePath</code>
or the CLI will write to the file <code>sequelize-meta.json</code>. If you want to keep the information in the
database, using <code>sequelize</code>, but want to use a different table, you can change the table name using
<code>migrationStorageTableName</code>.</p>
<pre><code class="lang-json"><code class="source-code prettyprint">{
  "development": {
    "username": "root",
    "password": null,
    "database": "database_development",
    "host": "127.0.0.1",
    "dialect": "mysql",

    // Use a different storage type. Default: sequelize
    "migrationStorage": "json",

    // Use a different file name. Default: sequelize-meta.json
    "migrationStoragePath": "sequelizeMeta.json",

    // Use a different table name. Default: SequelizeMeta
    "migrationStorageTableName": "sequelize_meta"
  }
}</code>
</code></pre>
<p><strong>Note:</strong> <em>The <code>none</code> storage is not recommended as a migration storage. If you decide to use it, be
aware of the implications of having no record of what migrations did or didn't run.</em></p>
<h4 id="seed-storage">Seed Storage</h4>
<p>By default the CLI will not save any seed that is executed. If you choose to change this behavior (!),
you can use <code>seederStorage</code> in the configuration file to change the storage type. If you choose <code>json</code>,
you can specify the path of the file using <code>seederStoragePath</code> or the CLI will write to the file
<code>sequelize-data.json</code>. If you want to keep the information in the database, using <code>sequelize</code>, you can
specify the table name using <code>seederStorageTableName</code>, or it will default to <code>SequelizeData</code>.</p>
<pre><code class="lang-json"><code class="source-code prettyprint">{
  "development": {
    "username": "root",
    "password": null,
    "database": "database_development",
    "host": "127.0.0.1",
    "dialect": "mysql",
    // Use a different storage. Default: none
    "seederStorage": "json",
    // Use a different file name. Default: sequelize-data.json
    "seederStoragePath": "sequelizeData.json",
    // Use a different table name. Default: SequelizeData
    "seederStorageTableName": "sequelize_data"
  }
}</code>
</code></pre>
<h3 id="configuration-connection-string">Configuration Connection String</h3>
<p>As an alternative to the <code>--config</code> option with configuration files defining your database, you can
use the <code>--url</code> option to pass in a connection string. For example:</p>
<pre><code class="lang-bash"><code class="source-code prettyprint">$ node_modules/.bin/sequelize db:migrate --url 'mysql://root:password@mysql_host.com/database_name'</code>
</code></pre>
<h3 id="connecting-over-ssl">Connecting over SSL</h3>
<p>Ensure ssl is specified in both <code>dialectOptions</code> and in the base config.</p>
<pre><code class="lang-json"><code class="source-code prettyprint">{
    "production": {
        "dialect":"postgres",
        "ssl": true,
        "dialectOptions": {
            "ssl": true
        }
    }
}</code>
</code></pre>
<h3 id="programmatic-use">Programmatic use</h3>
<p>Sequelize has a <a href="https://github.com/sequelize/umzug">sister library</a> for programmatically handling execution and logging of migration tasks.</p>
<h2 id="query-interface">Query Interface</h2>
<p>Using <code>queryInterface</code> object described before you can change database schema. To see full list of public methods it supports check <a href="/v4/class/lib/query-interface.js~QueryInterface.html">QueryInterface API</a></p>
</div>
</div>
</div>

<footer class="footer">
  Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(0.5.2)</span><img src="/v4/./image/esdoc-logo-mini-black.png"></a>
</footer>



<script src="/v4/script/pretty-print.js"></script>



<script src="/v4/script/patch-for-local.js"></script>


</body></html>